package com.fingard.dsp.bank.directbank.citic03;

import java.util.Calendar;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CITIC03Balance extends CITIC03Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        return null;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7505.getDateFrom());
            tmpCalendarFrom.add(Calendar.DATE, 1);

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7505.getDateTo());
            tmpCalendarTo.add(Calendar.DATE, 1);
            CITIC03Header tmpBankHeader = new CITIC03Header();
            tmpBankHeader.action = "DLGSAQRY";
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然日循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, tmpBankHeader);
                String specialAct = getBankConfig().getValueAsString("specialAct");
                String accNo = p_req7505.actNum;
                if (specialAct.contains("," + p_req7505.actNum + ",")) {
                    accNo = accNo + convertCurToBank(p_req7505.actCur);
                }
                xmlWriter.writeElementString("accNo", accNo);
                xmlWriter.writeElementString("startDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                xmlWriter.writeElementString("endDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                xmlWriter.writeElementString("pageNumber", "1");
                xmlWriter.writeElementString("startRecord", "1");
                xmlWriter.writeElementString("cryType", convertCurToBank(p_req7505.actCur));
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                XmlTextReader tmpBkReader;
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                        String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                        if (isActiveRetCode(tmpStatus)) {
                            if (!"PBRA001".equals(tmpStatus)) {
                                RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                                //交易日期
                                Calendar tmpBalDate = Calendar.getInstance();
                                tmpBalDate.setTime(tmpCalendarFrom.getTime());
                                tmpBalDate.add(Calendar.DATE, -1);
                                tmpRetDetail.balDate = Format.Date10Format.format(tmpBalDate.getTime());
                                String CDFlag = "C".equals(tmpBkReader.getFirstText("/stream/balanceDCFlag")) ? "" : "-";
                                //余额
                                tmpRetDetail.actBal = tmpBkReader.getFirstText("/stream/finalBalance");
                                if (StringHelper.hasAnyChar(tmpRetDetail.actBal)) {
                                    tmpRetDetail.actBal = CDFlag + tmpRetDetail.actBal;
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = StringHelper.isNullOrEmpty(tmpStatusText) ? tmpStatus : tmpStatusText;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
